﻿//
//    ___ _____ ___  ___ __  __ 
//   / __|_   _/ _ \| _ \  \/  |
//   \__ \ | || (_) |   / |\/| |
//   |___/ |_| \___/|_|_\_|  |_|
// 
//   Storm.TabControl.dll
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//     Storm.TabControl.dll was created under the LGPL license.
//     It was based on another .dll and evolved from that one.
//     The original .dll was created by Hadi Eskandari.
//
//   What is Storm?
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//     Storm is a set of dynamic link libraries used in Theodor
//     "Vestras" Storm Kristensen's application "Moonlite".
//     
//
//   Thanks:
//   ¯¯¯¯¯¯¯
//     - Hadi Eskandari for creating and publishing the library.
//
//
//   Copyright (c) Theodor "Vestras" Storm Kristensen 2009
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//

namespace Storm.TabControl.Helpers
{
    using System;
    using System.Collections;
    using System.ComponentModel;

    #region Delegates
    public delegate void CollectionClear();
    public delegate void CollectionChange(int index, object value);
    #endregion

    #region Base class
    public abstract class EventCollection : CollectionBase
    {
        #region Members
        private int _suspendCount;

        [Browsable(false)]
        public event CollectionClear Clearing;

        [Browsable(false)]
        public event CollectionClear Cleared;

        [Browsable(false)]
        public event CollectionChange Inserting;

        [Browsable(false)]
        public event CollectionChange Inserted;

        [Browsable(false)]
        public event CollectionChange Removing;

        [Browsable(false)]
        public event CollectionChange Removed;
        #endregion

        #region Members
        #region Public
        public void SuspendEvents()
        {
            _suspendCount++;
        }

        public void ResumeEvents()
        {
            _suspendCount--;
        }

        // Property
        [Browsable(false)]
        public bool IsSuspended
        {
            get { return (_suspendCount > 0); }
        }
        #endregion

        #region Protected
        protected override void OnClear()
        {
            if (!IsSuspended)
            {
                if (Clearing != null)
                    Clearing();
            }
        }

        protected override void OnClearComplete()
        {
            if (!IsSuspended)
            {
                if (Cleared != null)
                    Cleared();
            }
        }

        protected override void OnInsert(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Inserting != null)
                    Inserting(index, value);
            }
        }

        protected override void OnInsertComplete(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Inserted != null)
                    Inserted(index, value);
            }
        }

        protected override void OnRemove(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Removing != null)
                    Removing(index, value);
            }
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            if (!IsSuspended)
            {
                if (Removed != null)
                    Removed(index, value);
            }
        }

        protected int IndexOf(object value)
        {
            return List.IndexOf(value);
        }
        #endregion
        #endregion

        /// <summary>
        /// Initializes the EventCollection
        /// </summary>
        public EventCollection()
        {
            _suspendCount = 0;
        }
    }
    #endregion
}